#define WIN32_LEAN_AND_MEAN
#include <WinSock2.h>
#include <Windows.h>
#include"mysocket.h"
#include <fstream>
#include <sstream>
#include <locale>
using namespace std;
#pragma comment(lib, "ws2_32.lib")

//数据库用户名密码通信端

int main(int argc, char* argv[])
{
	// 设置控制台代码页为UTF-8
	SetConsoleOutputCP(65001);
	SetConsoleCP(65001);
	
	// 设置本地化环境
	std::locale::global(std::locale(".UTF8"));
	std::wcout.imbue(std::locale());
	std::wcin.imbue(std::locale());
	string scriptFile = "";
	
	// 解析命令行参数
	for (int i = 1; i < argc; i++) {
		string arg = argv[i];
		if (arg == "--script" && i + 1 < argc) {
			scriptFile = argv[i + 1];
			i++; // 跳过下一个参数
		}
	}

	// 创建数据库客户端实例
	DatabaseClient* dbClient = new DatabaseClient;
	cout << "=== Database Client Started Successfully ===" << endl;
	
	if (!scriptFile.empty()) {
		cout << "[INFO] Script mode started, will execute script file: " << scriptFile << endl;
	}

	// 初始化客户端
	if (!dbClient->initialize()) {
		cout << "Client initialization failed!" << endl;
		delete dbClient;
		return -1;
	}

	// 连接到数据库服务器
	if (!dbClient->connectToServer("127.0.0.1", 8080)) {
		cout << "Failed to connect to database server!" << endl;
		delete dbClient;
		return -1;
	}

	// 启动数据库会话（交互式或脚本模式）
	if (!scriptFile.empty()) {
		dbClient->startScriptSession(scriptFile);
	} else {
		dbClient->startSession();
	}

	// 清理客户端资源
	delete dbClient;
	return 0;
}

